<template>
  <div>
    <a-form layout="inline" class="default-search-form gb-flex-form" :form="form" @submit="handleSearch">
      <a-row :gutter="8">
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" label="请求路径">
            <a-input v-decorator="['url']" placeholder="请求路径"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" label="IP地址">
            <a-input v-decorator="['ip']" placeholder="IP地址"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" label="请求内容">
            <a-input v-decorator="['textSearch']" placeholder="请求内容"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" label="Json Path">
            <a-input v-decorator="['jsonPathSearch']" placeholder="$.a[*] ? (@ > 2)"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item class="gb-search-button">
            <a-button class="item" v-if="funcs['AuditLogSearch']" type="primary" html-type="submit">
              {{ $t(funcs['AuditLogSearch']) }}
            </a-button>
            <a-button type="primary" class="item" @click="handleReset">
              {{ $t('reset') }}
            </a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <a-table :columns="columns"
      :rowKey="record => record.id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      size="small"
      :scroll="{ x: 1000 }"
    >
      <div slot="param" slot-scope="text, record">
        <AuditLogView :row="record" />
      </div>
    </a-table>
  </div>
</template>
<script>
import axios from 'axios'
import common from '@/global/common.js'

export default {
  data() {
    return {
      form: this.$form.createForm(this, {name: this._uid}),
      data: [],
      loading: true,
      pagination: common.getPagination(),
    }
  },
  computed: {
    funcs() {
      return common.getVisibleFunc('AuditLogList')
    },
    columns() {
      const columns = [
        { title: '请求路径', dataIndex: 'url', sorter: true, key: 'url', customRender: (text) => { return <LongTableColumn>{text}</LongTableColumn> }, width: 220,  },
        { title: 'IP地址', dataIndex: 'ip', sorter: true, key: 'ip', width: 120, },
        { title: '请求内容', dataIndex: 'param', scopedSlots: { customRender: 'param' }, },
        { title: this.$t("create_by"), dataIndex: 'createByDesc', sorter: true, key: 'create_by', customRender: (text) => { return <LongTableColumn>{text}</LongTableColumn> }, width: 100, }, 
        { title: this.$t("create_time"), dataIndex: 'createTime', sorter: true, key: 'create_time', width: 155, }, 
      ]
      return columns
    }

  },
  mounted() {
    this.fetch()
  },
  methods: {
    handleSearch (e) {
      if(e){
        this.pagination.current = 1
        e.preventDefault()
      }
      this.fetch()
    },
    handleReset () {
      this.form.resetFields()
      this.handleSearch()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination.pageSize = pagination.pageSize
      this.pagination.current = pagination.current
      this.pagination.sortField = ''
      this.pagination.sortOrder = ''
      if (sorter.column) {
        this.pagination.sortField = sorter.columnKey
        this.pagination.sortOrder = sorter.order
      }
      this.fetch({
        ...filters,
      })
    },
    fetch (param = {}) {
      this.form.validateFields((err, formValue) => {
        if (err) {
          return
        }
        this.loading = true
        axios({
          url: '/infra/auditLog/find',
          method: 'get',
          params: {
            pageSize: this.pagination.pageSize,
            pageNum: this.pagination.current,
            sortField: this.pagination.sortField,
            sortOrder: this.pagination.sortOrder,
            ...param,
            ...formValue
          },
        }).then((res) => {
          this.pagination.total = res.data.total
          this.data = res.data.rows
        }).catch(() => {
          
        }).finally(() => {
          this.loading = false
        })
      })
    }
  },
}
</script>